<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
	"http://www.w3.org/TR/html4/loose.dtd">

<!--

Copyright (C) 2012 Jim Xochellis
(homepage: https://sites.google.com/site/xochellis/)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

<html>

<head>
	<meta http-equiv= "Content-Type" content= "text/html;charset=UTF-8">
	<link type= "text/css" rel="stylesheet" href= "details/wizard.css">
	<link rel= "icon" href= "favicon.ico" type= "image/x-icon">
	<link rel= "shortcut icon" href= "favicon.ico" type= "image/x-icon">
	
	<title>Online Generic Data Ordering Wizard</title>
</head>

<body>

<?php

//Initialize global state variables (_POST is only accessed from here)

$DEBUG_OUTPUT = FALSE;
$DB_SUPPORT = FALSE;

//basic_cfg.php overrides the basic settings
include "config/basic_cfg.php";

//First thing: Set basic wizard mode

if ( isset($_POST["create"]) )
	$wizard_mode = "create";
elseif ( isset($_POST["instructions"]) )
	$wizard_mode = "instructions";
elseif ( isset($_POST["example"]) )
	$wizard_mode = "example";
else
	$wizard_mode = "reset";

//Now set all global variables

if ( ($wizard_mode == "create") || ($wizard_mode == "instructions") )
{
	//Retain user choices in the form...
	$functor_name	= isset($_POST["functor_name"])? $_POST["functor_name"]: "";		
	$field_name		= isset($_POST["field_name"])? $_POST["field_name"]: "";
	$struct_type	= isset($_POST["struct_type"])? $_POST["struct_type"]: "";
	
	$order_is_ascending		= ! isset($_POST["ordering_type"]) || ($_POST["ordering_type"] == "ascending");
	$use_const_reference	= (isset($_POST["check_const"]) && ($_POST["check_const"] == "const"));
}
elseif ($wizard_mode == "example")
{
	$use_const_reference	= TRUE;
	$functor_name			= "point_functor";
	$struct_type			= "point";
	$order_is_ascending		= TRUE;
	$field_name				= "x y";
}
else //reset or initial creation
{
	$use_const_reference	= TRUE;
	$functor_name			= "functor";
	$struct_type			= "";
	$order_is_ascending		= TRUE;
	$field_name				= "";
}

if ( $DEBUG_OUTPUT )
{
	echo "wizard_mode: ", $wizard_mode, "<br>";
	echo "order_is_ascending: ", $order_is_ascending == TRUE, "<br>";
	echo "use_const_reference: ", $use_const_reference == TRUE, "<br>";
	echo "<hr>";
}

?>

<table class="rounded-corners" style="width: 100%; border-spacing: 0px; background-color: LightSteelBlue;">
<tbody>

<!-- Main page title bar -->
<tr><td style= "width: 100%; text-align: center;">
<strong><big>Online Generic Data Ordering Wizard (for C++)</big></strong>
</td></tr>

<tr><td>

<table class="rounded-corners" style= "width: 100%; border-spacing: 0px; background-color: LightGoldenRodYellow;">
<tbody>

<tr><td>&nbsp;</td></tr>

<!-- The wizard form definition -->

<tr><td align="center" style= "border-spacing: 0px;">
<form method="POST" action="<?echo $_SERVER["PHP_SELF"]?>">

<table class="rounded-corners" style="border: 1px black solid; background-color: LightSteelBlue;">
<tbody>

<tr>
	<th align="right">Type&nbsp;of&nbsp;ordering</th>
	
	<td><table width="100%"><tbody><tr>
		<td><input type="radio" name="ordering_type" value="ascending" <?CheckButton($order_is_ascending)?> > Ascending</td>
		<td><input type="radio" name="ordering_type" value="descending" <?CheckButton(! $order_is_ascending)?> > Descending</td>
		<td align="right"><em>Created by <a href="https://sites.google.com/site/xochellis/" target="_blank">Jim Xochellis</a></em></td>
	</tr></tbody></table></td>
</tr>
<tr>
	<th align="right">Functor&nbsp;name</th>
	<td align="left"><input type="text" name="functor_name" size="32"
		title="Enter the name of the functor structure itself" value="<?echo $functor_name?>"></td>
</tr>
<tr>
	<th align="right">Parameter&nbsp;type</th>
	<td align="left">
		<input type="text" name="struct_type" size="32"
			   title="Enter the type of the struct or class parameter" value="<?echo $struct_type?>">
		<input type="checkbox" name="check_const" value="const" <?CheckButton($use_const_reference)?> > Const reference
	</td>
</tr>
<tr>
	<th align="right">Member&nbsp;variables<br>or&nbsp;methods</th>
	<td align="left"><input type="text" name="field_name" size="64"
			title="Enter up to four member variable or method names in order of significance" value="<?echo $field_name?>"></td>
</tr>

<tr>
	<td align="right">
		<input type="submit" name="create" title="Produce code" value="Create">
		<input type="submit" name="reset" title="Reset the wizard" value="Reset">
	</td>
	<td align="right">
		<input type="submit" name="instructions" title="Provide usage instructions" value="Instructions">
		<input type="submit" name="example" title="Provide usage example" value="Example">
		&nbsp;&nbsp;
	</td>
</tr>

</tbody>
</table>
</form>
</td></tr>

<tr><td>

<?php

// Basic wizard mode handling

switch ( $wizard_mode )
{
	case "create":
		require "details/functor_creator.php";	
		if ( ! ShowImplementationCode($order_is_ascending, $functor_name, $struct_type, 
						$use_const_reference, $field_name, $DB_SUPPORT) )
		{
			// On failure show instructions
			ShowInstructions();
		}
		break;
	
	case "example":
		require "details/functor_creator.php";
		ShowExampleDescription();
		ShowImplementationCode($order_is_ascending, $functor_name, $struct_type, $use_const_reference, $field_name);	
		break;
	
	case "instructions":
		ShowInstructions();
		break;

	default:
		ShowIntroduction();
}

// -------------------------------------------------------------
// Helper functions

function ShowIntroduction()
{
	$info_mode = isset($_GET["info"])? $_GET["info"]: "intro";

	$tabs_desc_array = array
	(
		array("id" => "intro",	"name" => "Overview"),
		array("id" => "usage",	"name" => "Usage Instructions"),
		array("id" => "stl",	"name" => "STL compatibility"),
		array("id" => "qna",	"name" => "Interesting Q&amp;A")
	);
	
	echo "<br><br>\n\n";	
	echo "<table class=\"tabs-header\" cellspacing=\"0\" cellpadding=\"2\"><tbody><tr>\n";
	
	foreach ($tabs_desc_array as $tab_desc)
	{
		if ($tab_desc["id"] == $info_mode)
			echo "<td class=\"selected\">", $tab_desc["name"], "</td>\n";
		else
			echo "<td><a href=\"?info=", $tab_desc["id"], "\">", $tab_desc["name"], "</a></td>\n";
	}
	
	echo "</tr></tbody></table>\n\n";

	echo "<div style=\"border: 1px solid black; background-color: white;\">\n";
	
	switch ( $info_mode )
	{
		case "stl":
			require "details/info_stl.html";
			break;
		
		case "qna":
			require "details/info_qna.html";
			break;
		
		case "usage":
			require "details/instructions.html";
			break;
			
		default:
			require "details/info_intro.html";
	}		
		
	echo "<br></div>";
	
}

function ShowInstructions()
{
	echo "<h3>Usage Instructions</h3>\n";
	echo "<div class=\"sub-box\">\n";

	//Include the instructions html code
	require "details/instructions.html";
	
	echo "<br>\n";
	echo "</div>\n";
}

function ShowExampleDescription()
{
	//Include the example html code
	require "details/example.html";
}

function CheckButton($do_check)
{
	if ( ! empty($do_check) )
		echo "checked=\"checked\"";
}

?>

</td></tr>

<tr><td>&nbsp;</td></tr>

</tbody>
</table>

</td></tr>
</tbody>
</table>

</body>
</html>
